package com.ourchem.bus.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ourchem.bus.domain.CollectManage;
import com.ourchem.bus.domain.Course;
import com.ourchem.bus.domain.Evaluate;
import com.ourchem.bus.mapper.CollectManageMapper;
import com.ourchem.bus.mapper.CourseMapper;
import com.ourchem.bus.mapper.EvaluateMapper;
import com.ourchem.bus.service.ICourseService;
import com.ourchem.common.utils.DateUtils;
import com.ourchem.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ourchem.bus.mapper.CourseMapper;
import com.ourchem.bus.domain.Course;
import com.ourchem.bus.service.ICourseService;

/**
 * courseService业务层处理
 *
 * @author xiYan
 * @date 2023-01-12
 */
@Service
public class CourseServiceImpl implements ICourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private CollectManageMapper collectManageMapper;

    @Autowired
    private EvaluateMapper evaluateMapper;

    /**
     * 查询course
     *
     * @param id course主键
     * @return course
     */
    @Override
    public Course selectCourseById(Long id) {
        Course course = courseMapper.selectCourseById(id);
        CollectManage collectManage = new CollectManage();
        collectManage.setUserId(SecurityUtils.getUserId());
        collectManage.setCollectId(id.toString());
        List<CollectManage> list = collectManageMapper.selectCollectManageList(collectManage);
        if (list.size() > 0) {
            course.setRemark("1");
        } else {
            course.setRemark("0");
        }
        return course;
    }

    /**
     * 查询course列表
     *
     * @param course course
     * @return course
     */
    @Override
    public List<Course> selectCourseList(Course course) {
        List<Course> courses = courseMapper.selectCourseList(course);
        CollectManage collectManage = new CollectManage();
        courses.stream().map(v -> {
            collectManage.setUserId(SecurityUtils.getUserId());
            collectManage.setCollectId(v.getId().toString());
            if (collectManageMapper.selectCollectManageList(collectManage).size() > 0) {
                v.setRemark("1");
            } else {
                v.setRemark("0");
            }
            return v;
        }).collect(Collectors.toList());

        return courses;
    }

    /**
     * 新增course
     *
     * @param course course
     * @return 结果
     */
    @Override
    public int insertCourse(Course course) {
        course.setCreateTime(DateUtils.getNowDate());
        return courseMapper.insertCourse(course);
    }

    /**
     * 修改course
     *
     * @param course course
     * @return 结果
     */
    @Override
    public int updateCourse(Course course) {
        course.setUpdateTime(DateUtils.getNowDate());
        return courseMapper.updateCourse(course);
    }

    /**
     * 批量删除course
     *
     * @param ids 需要删除的course主键
     * @return 结果
     */
    @Override
    public int deleteCourseByIds(Long[] ids) {
        CollectManage collectManage = new CollectManage();
        collectManage.setType("2");
        Evaluate evaluate = new Evaluate();
        evaluate.setType(2L);
        /***
         * 关联删除评论和收藏
         * */
        for (Long id :
                ids) {
            collectManage.setCollectId(id.toString());
            evaluate.setRelationId(id.longValue());
            List<CollectManage> list = collectManageMapper.selectCollectManageList(collectManage);
            List<Evaluate> list2 = evaluateMapper.selectEvaluateList(evaluate);
            if(list.size()==1){
                collectManageMapper.deleteCollectManageById(list.get(0).getId());
            }
            if(list2.size()==1){
                evaluateMapper.deleteEvaluateById(list2.get(0).getId());
            }
        }
        return courseMapper.deleteCourseByIds(ids);
    }

    /**
     * 删除course信息
     *
     * @param id course主键
     * @return 结果
     */
    @Override
    public int deleteCourseById(Long id) {
        return courseMapper.deleteCourseById(id);
    }
}
